package com.bitdf.txing.group.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bitdf.txing.common.enume.TxCodeEnume;
import com.bitdf.txing.common.interceptor.LoginInterceptor;
import com.bitdf.txing.common.to.UserTo;
import com.bitdf.txing.common.utils.MyException;
import com.bitdf.txing.common.utils.PageUtils;
import com.bitdf.txing.common.utils.R;
import com.bitdf.txing.common.vo.UserInfoVo;
import com.bitdf.txing.group.entity.GroupCollectEntity;
import com.bitdf.txing.group.feign.EsFeignService;
import com.bitdf.txing.group.service.GroupCollectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;


/**
 * @author lizhiwei
 * @email 123@qq.com
 * @date 2023-01-31 01:03:06
 */
@RestController
@RequestMapping("${txing.request.path.group}/collect")
public class GroupCollectController {
    @Autowired
    private GroupCollectService groupCollectService;

    @Autowired
    EsFeignService esFeignService;

    /**
     * 列表
     */
    @SaCheckLogin
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = groupCollectService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @SaCheckLogin
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Integer id) {
        GroupCollectEntity groupCollect = groupCollectService.getById(id);

        return R.ok().put("groupCollect", groupCollect);
    }

    /**
     * 收藏(保存在数据库+修改es中组团收藏数，注意：数据库中组团收藏数没改)
     */
    @GetMapping("/save")
    @SaCheckLogin
    @Transactional(rollbackFor = Exception.class)
    public R save(@RequestParam("groupId") Long groupId) {
        UserInfoVo userInfoVo = (UserInfoVo) StpUtil.getSession().get("user");
        //TODO 检查组团是否存在
//        GroupCollectEntity groupCollectEntity = new GroupCollectEntity(null,userTo.getId(),groupId,new Date());
        GroupCollectEntity groupCollectEntity = new GroupCollectEntity();
        groupCollectEntity.setUserId(userInfoVo.getId());
        groupCollectEntity.setGroupId(groupId);
        boolean save = groupCollectService.save(groupCollectEntity);
        //远程调用更新es中的数据
        R r = esFeignService.countIncreOrDcre(groupId, 1, "collectCount");
        return save && r.getCode() == 0 ? R.ok() : R.error(TxCodeEnume.COMMON_SYSTEM_UNKNOWN_EXCEPTION);
    }

    /**
     * 修改
     */
    @SaCheckLogin
    @RequestMapping("/update")
    public R update(@RequestBody GroupCollectEntity groupCollect) {
        groupCollectService.updateById(groupCollect);
        return R.ok();
    }

    /**
     * 取消收藏
     */
    @PostMapping("/delete")
    @Transactional(rollbackFor = Exception.class)
    @SaCheckLogin
    public R delete(@RequestBody Long[] ids) {
        List<GroupCollectEntity> list = groupCollectService.list(new QueryWrapper<GroupCollectEntity>().in("id", ids));
        //删除数据库中的相关数据
        boolean b = groupCollectService.removeByIds(Arrays.asList(ids));
        //遍历更新es中的数据
        for (GroupCollectEntity entity : list) {
            R r = esFeignService.countIncreOrDcre(entity.getGroupId(), -1, "collectCount");
            if (r.getCode() != 200) {
                throw new MyException("更新es部分出现异常", 9001);
            }
        }
        return R.ok();
    }

}
